#include "llt_v6mapper.hh"

using namespace userFunction;

void
v6mapper::processData(lee::PktData * const pkt)
{
  tt::SixPacket * v6Pkt;
  v6Pkt = static_cast<tt::SixPacket *>(pkt);

  uint32_t destAddr = v6Pkt->ipHdr.ip6_dst.s6_addr32[0];

  if(!mapExists(v6Pkt->ipHdr.ip6_src, destAddr)) {
    natpt::V6NewMapEvent * evt = new natpt::V6NewMapEvent(v6Pkt->ipHdr.ip6_src, destAddr);
    pub_natpt_V6NewerMapEvent(evt);
  }
  normal(pkt);

}

bool
v6mapper::mapExists(in6_addr v6src, uint32_t v4dst)
{
  bool retvalue;
  if(addrMap.find(v6src) == addrMap.end()) {
    std::set<uint32_t> * newSet = new std::set<uint32_t>;
    newSet->insert(v4dst);
    addrMap[v6src] = newSet;
    retvalue = false;
  }
  else {
    std::set<uint32_t> * targetSet = addrMap.find(v6src)->second;
    // insert returns a pair whose second element says whether or not
    // the element was actually inserted.
    if(targetSet->insert(v4dst).second) {
      retvalue = false;
    }
    else {
      retvalue = true;
    }
  }
  return retvalue;
}
